<?php 
class model_report extends Model
{
	
	function construct()
	{
		parent::__construct();
	}

	function client_hours_logged($range = array())
	{

		$sql = "select
					c.id
					,c.company_name
					,j.job_end as je
					,j.job_start as js
					,timediff(j.job_end,j.job_start) as spent
					,sum(timediff(j.job_end,j.job_start)) as spent2
					, count(j.id) as numJobs
				from company c
				
				left join job j 
					on c.id = j.company_id
				
				where 
					j.job_end is not null 
					and j.job_end != '0000-00-00 00:00:00'
					and (j.job_start > '". $range[0] ."' and j.job_end  <= '". $range[1] ."')
					and j.active =1
				group by c.id;";
		//dump($sql);
		$r =  $this->db->query($sql);
		
		return $r;
	}
	
	function job_get_jos($range = array())
	{
		
	}
	
	function user_hours($range = array())
	{
		$sql = "SELECT u.*

				,sum(timediff(j.job_end,j.job_start)) as spent2
				
				FROM user u
				
				left join job j on j.job_logged_by = u.id
				
				where 
					j.job_end is not null 
					and j.job_end != '0000-00-00 00:00:00'
					and (j.job_start > '". $range[0] ."' and j.job_end  < '". $range[1] ."')
					and j.active =1
				
				group by u.id";
		$r =  $this->db->query($sql);
		
		return $r;
	}
	
	
	function client_view($range = array())
	{
		//===== get company id ======//
		
		$c = $this->db->query("select company_id from user where id=". $_SESSION['user_id']);
		$c_r = $c->row_array();
		
		$cid = $c_r['company_id'];
		//==========================//
		
		/*$sql = "select
					c.id
					,j.*
					,c.company_name
					,j.job_end as je
					,j.job_start as js
					,timediff(j.job_end,j.job_start) as spent
					#,sum(timediff(j.job_end,j.job_start)) as spent2
					, count(j.id) as numJobs
				from company c
				
				left join job j 
					on c.id = j.company_id
				
				where 
					j.job_end is not null 
					and j.job_end != '0000-00-00 00:00:00'
					and (j.job_start > '". $range[0] ."' and j.job_end  <= '". $range[1] ."')
					and (j.company_id=". $cid .")
					and j.active =1
				group by c.id;";*/
		//dump($sql);
		$r =  $this->db->query($sql);
		
		return $r;
	}
}